package top.healthylife.gateway.platform.mysql.api.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.utils.SystemThreadLocal;
import top.healthylife.basics.utils.utils.TenantContextHolder;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.mysql.api.config.SystemConfig;
import top.healthylife.gateway.platform.mysql.api.entity.BusinessBlacklist;
import top.healthylife.gateway.platform.mysql.api.mapper.BusinessBlacklistMapper;
import top.healthylife.gateway.platform.mysql.dto.bblack.req.MySqlReqBusinessBlackListDto;
import top.healthylife.gateway.platform.mysql.dto.bblack.res.MySqlResBusinessBlackListDto;
import top.heathylife.gateway.common.cons.SystemCons;
import top.heathylife.gateway.common.po.RecordRequestInfo;

import java.util.List;


/**
 * @author GaoZexi
 * @datetime 2020/3/20 13:19
 */
@Slf4j
@Service
@ApiServiceExplain(price = 0, apiName = "企业黑名单", info = "根据条件查询企业黑名单的数据")
@AllArgsConstructor
public class BusinessBlackApi implements ParentFace<MySqlReqBusinessBlackListDto, MySqlResBusinessBlackListDto> {

    BusinessBlacklistMapper businessBlacklistMapper;

    @Override
    public MySqlResBusinessBlackListDto execute(MySqlReqBusinessBlackListDto t) throws Exception {
        SystemConfig baseConfig = (SystemConfig) SystemConfigUtils.get();
        String name = baseConfig.getName();
        //切换数据源
        log.info("切换数据源{}", name);
        DynamicDataSourceContextHolder.push(name);
        TenantContextHolder.clear();
        LambdaQueryWrapper<BusinessBlacklist> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(t.getBusinessName()), BusinessBlacklist::getBusinessName, t.getBusinessName())
                .eq(ObjectUtil.isNotEmpty(t.getCorporateIdentityCard()), BusinessBlacklist::getCorporateIdentityCard, t.getCorporateIdentityCard())
                .eq(ObjectUtil.isNotEmpty(t.getSocialUnifiedCredit()), BusinessBlacklist::getSocialUnifiedCredit, t.getSocialUnifiedCredit())
                .eq(BusinessBlacklist::getRemoveBlacklist, "0")
                .eq(BusinessBlacklist::getArrearsStatus, "0");
        String body = JSONObject.toJSONString(t);
        log.info("企业黑名单,查询条件[{}]", body);
        RecordRequestInfo.recordRequest(name + "(数据源名称)", body, null, "数据库查询请求");
        List<BusinessBlacklist> businessBlacklists = businessBlacklistMapper.selectList(wrapper);
        MySqlResBusinessBlackListDto mySqlResBusinessBlackListDto;
        if (ObjectUtil.isEmpty(businessBlacklists)) {
            log.info("企业黑名单,查询结果[未命中]");
            mySqlResBusinessBlackListDto = new MySqlResBusinessBlackListDto(false, "未命中");
            SystemThreadLocal.set(SystemCons.ORIGIN_DATA, JSONObject.toJSONString(mySqlResBusinessBlackListDto));
            return mySqlResBusinessBlackListDto;
        }
        BusinessBlacklist businessBlacklist = businessBlacklists.get(0);
        //将三方元数据存储到线程变量
        SystemThreadLocal.set(SystemCons.ORIGIN_DATA, JSONObject.toJSONString(businessBlacklist));
        //返回数据
        MySqlResBusinessBlackListDto copy = BeanUtil.copyProperties(businessBlacklist, MySqlResBusinessBlackListDto.class);
        copy.setMatch(true).setMessage("命中");
        log.info("企业黑名单,查询结果[命中],数据=>[{}]", JSONObject.toJSONString(copy));
        return copy;
    }
}
